<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Home_Home extends Controller_Portal {

    public function action_index() {

        $this->template->content = View::factory('home/index');
    }

    public function action_register() {
        
        //echo "<pre>";var_dump($a);die;
        if ($_POST) {
            
            $s_user = new Model_Service_User();
            $en_user = $s_user->get_by_location_id($this->_user['id'])->get_data();            
            if ($en_user->getId() > 0) {
                
                //update
                $a_post = $this->request->post();
                echo"<pre>";
                var_dump($a_post);
                die;
                $en_user->setFname($a_post['fullname']);
                $s_user->update($en_user);
            } else {
                
                //insert
                $checkError = array();
                $fullname = strip_tags($_POST['fullname']);
                if(isset($fullname) && !empty($fullname)){
                    $en_user->setFname($fullname);
                    
                }  else {
                    $checkError['fullname'] = 'Fullname not empty!';
                }
                $en_user->setGender(filter_var($_POST['sex']));
                $birthday = strip_tags($_POST['birthday']);
                if(isset($birthday) && !empty($birthday)){
                    $en_user->setdob($birthday);
                }  else {
                    $checkError['birthday'] = "Birthday not empty";
                }
                $email = strip_tags($_POST['email']);
                if(filter_var($email,FILTER_VALIDATE_EMAIL)){
                    $en_user->setEmail($email);
                }  else {
                    $checkError['email'] = "Email not empty";
                }
                $address = strip_tags($_POST['address']);
                if(isset($address) && !empty($address)){
                    $en_user->setOffice_Location($address);
                }  else {
                    $checkError['address'] = "Address not empty";
                }
                $city = strip_tags($_POST['city']);
                if(isset($city) && !empty($city)){
                    $en_user->setCity($city);
                }  else {
                    $checkError['city'] = "City not empty";
                }
                $phone = strip_tags($_POST['phone']);
                if(filter_var($phone,FILTER_VALIDATE_INT)){
                    $en_user->setPhone($phone);
                }  else {
                    $checkError['phone'] = "Phone not empty";
                }
                //var_dump(!empty($checkError));die;
                if(empty($checkError)){
                    $s_user->insert($en_user);
                }  else {
                    var_dump($checkError);die;
                }
                
            }
        }
        $this->template->content = View::factory('home/register');
    }

}
